package com.uminho.egendmeprofessional;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.uminho.objects.Resource;
import com.uminho.objects.Service;
import com.uminho.objects.Task;

public class ServiceDialog extends Dialog {
	Context c;
	Service s;
	Long bookmarkId;
	DialogEventListener dialogEventListener;


	public ServiceDialog(Context context, Service s, Long bookmarkId) {
		super(context,R.style.WindowTitleBackground);
		this.c=context;
		this.s = s;
		this.bookmarkId = bookmarkId;
		
		//By default, do nothing.
		this.dialogEventListener = new DialogEventListener() {
			
			public void onRemove(Dialog d, long fieldOne) {
			}
		};
	}
	
	public ServiceDialog(Context context, Service s, Long bookmarkId, DialogEventListener dialogEventListener) {
		this(context,s,bookmarkId);
		this.dialogEventListener = dialogEventListener;
	}

	@Override
	protected void onStart() {
		super.onStart();
		requestWindowFeature(Window.FEATURE_NO_TITLE); //Hides Title

		setContentView(R.layout.servico); //Apply layout

		/* Set fixed Views */
		ImageView avatar = (ImageView) findViewById(R.id.imageAvatar);
		if(s.getAvatar() != null) avatar.setImageDrawable(s.getAvatar());

		TextView title = (TextView) findViewById(R.id.title);
		title.setText(s.getName());

		TextView cat = (TextView) findViewById(R.id.category);
		//TODO change fake category
		String[] cats = this.getContext().getResources().getStringArray(R.array.categorieslist);
		cat.setText(cats[s.getCategory_id()]+" ");

		RatingBar rBar = (RatingBar) findViewById(R.id.ratingBar1);
		rBar.setRating(s.getRating());

		/* Set views programmatically */
		ViewGroup tabParent = (ViewGroup) findViewById(R.id.tabParent);
		ViewGroup profilePanel = (ViewGroup) findViewById(R.id.profilePanel);

		addField(profilePanel, "Address", s.getAddress(), Linkify.MAP_ADDRESSES);
		addDescriptiveField(profilePanel, "Description", s.getDescription());

		ViewGroup contactsPanel = addTab(tabParent,"Contacts");
		addField(contactsPanel, "E-mail", s.getEmail(),Linkify.EMAIL_ADDRESSES);
		addField(contactsPanel, "Mobile Phone", s.getMobile_phone(),Linkify.PHONE_NUMBERS);
		addField(contactsPanel, "Phone", s.getPhone(),Linkify.PHONE_NUMBERS);

		if(!s.getTasks().isEmpty()){
			ViewGroup tasksPanel = addTab(tabParent,"Tasks");

			for(int i=0; i<s.getTasks().size();i++){

				if(i!=0)
					addSeparator(tasksPanel);

				Task t = s.getTasks().get(i);

				addField(tasksPanel, "Name", t.getName());
				addField(tasksPanel, "Duration", t.getDuration().toString()+" min");
				addField(tasksPanel, "Price", t.getPrice().toString()+" €");
				addField(tasksPanel, "Capacity", t.getMax_people().toString()+" people");
				addDescriptiveField(tasksPanel, "Description", t.getDescription());


			}
		}

		if(!s.getResources().isEmpty()){
			ViewGroup resPanel = addTab(tabParent,"Resources");
			for(int i=0; i<s.getResources().size();i++){
				if(i!=0)
					addSeparator(resPanel);

				Resource r = s.getResources().get(i);

				addField(resPanel,"Name",r.getName());
				addDescriptiveField(resPanel,"Description",r.getDescription());


			}
		}
		
		setOnClickListeners();

	}


	private void addField(ViewGroup parent, String label, String value, Integer link){
		if(value == null || value.length()==0)
			return;

		View v = LayoutInflater.from(getContext()).inflate(R.layout.dialogfield, parent, false);

		TextView textView = (TextView) v.findViewById(R.id.label);
		textView.setText(label+" ");
		textView = (TextView) v.findViewById(R.id.value);
		textView.setText(value);
		if(link!=null)
			textView.setAutoLinkMask(link);

		parent.addView(v);
	}

	private void addField(ViewGroup parent, String label, String value){
		addField(parent,label,value, null);
	}

	private void addDescriptiveField(ViewGroup parent, String label, String value){
		if(value == null || value.length()==0)
			return;

		View v = LayoutInflater.from(getContext()).inflate(R.layout.dialogdfield, parent, false);

		TextView textView = (TextView) v.findViewById(R.id.label);
		textView.setText(label+" ");
		textView = (TextView) v.findViewById(R.id.value);
		textView.setText(value);

		parent.addView(v);
	}

	private ViewGroup addTab(ViewGroup parent, String handleText){
		if(handleText == null || handleText.length()==0)
			return null;

		View v = LayoutInflater.from(getContext()).inflate(R.layout.dialogtab, parent, false);
		TextView textView = (TextView) v.findViewById(R.id.textHandle);
		textView.setText(handleText);

		ViewGroup panel = (ViewGroup) v.findViewById(R.id.panel);

		parent.addView(v);
		return panel;
	}

	private void addSeparator(ViewGroup parent){
		View v = LayoutInflater.from(getContext()).inflate(R.layout.dialogseparator, parent, false);
		parent.addView(v);
	}

	private void setOnClickListeners(){
		Button buttonRemoveFromFavorites = (Button) findViewById(R.id.buttonRmFavorites);
		buttonRemoveFromFavorites.setEnabled(true);
		buttonRemoveFromFavorites.setVisibility(View.VISIBLE);
		final Dialog t = this;
		buttonRemoveFromFavorites.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(c);
				builder.setMessage(c.getString(R.string.realy_want_delete))
				.setCancelable(false)
				.setPositiveButton(c.getString(R.string.yes), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						
						dialogEventListener.onRemove(t,bookmarkId);
						
					}
				})
				.setNegativeButton(c.getString(R.string.no), new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();	        				       			
					}
				});
				AlertDialog alert = builder.create();
				alert.show();

			}
		});
	}
}
